home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / PROGRAMR / WINEXIT.ZIP / WINEXIT.C < prev    next >
C/C++ Source or Header  |  1993-07-19  |  18KB  |  617 lines

  1. /*
  2.  * $Id: WINEXIT.C 1.0 1993/07/19 15:26:48 nino Exp $
  3.  *
  4.  * Copyright (c) 1993.  Nino Margetic.  All rights reserved.
  5.  *
  6.  *      General permission is hereby granted to copy this
  7.  *      program freely provided no charge is made for its
  8.  *      distribution and provided this copyright notice
  9.  *      remains in place.
  10.  *
  11.  * Abstract:    WINEXIT.C - Exit from Windows from an icon.
  12.  *
  13.  *    This program provides an easy way to get out of Windows.
  14.  *    It can be run either from the Program Manager or it can
  15.  *    be started and minimized so that its icon shows up in
  16.  *    the icon area.
  17.  *
  18.  * Operating procedures:
  19.  *
  20.  *    This program can be placed in a convenient group in the
  21.  *    Program Manager or it can be placed in the StartUp group
  22.  *    so that it shows up in the main icon area.  If it is put
  23.  *    in the StartUp group it should probably be run minimized.
  24.  *
  25.  * Written: 16-Jul-1993 Nino Margetic <nino@medphys.ucl.ac.uk>
  26.  *
  27.  * Acknowledgements:
  28.  *        Thanks to Bruce C. Wright and his EXIT utility which served
  29.  *        as a template for this exercise in Windows programming.
  30.  *
  31.  *
  32.  */
  33.  
  34. #define STRICT
  35. #define WIN31
  36.  
  37. #include <windows.h>
  38. #include <bwcc.h>
  39. #ifdef __BORLANDC__
  40. #pragma hdrstop
  41. #endif
  42.  
  43. #include "winexit.h"
  44.  
  45.  
  46.  
  47. /*
  48.  * Main procedure
  49.  */
  50. int PASCAL WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  51.             LPSTR lpszCmdLine, int nCmdShow)
  52. {
  53.     MSG        msg;
  54.     UINT        fuErr;
  55.     WORD        winVer;
  56.  
  57.  
  58.     // save the program instance
  59.     hInst = hInstance;
  60.  
  61.     // turn off Windows error "file not found" box.
  62.         // HOWEVER, it doesn't seem to work for BWCC.DLL ...
  63.     fuErr = SetErrorMode ( SEM_NOOPENFILEERRORBOX ) ;
  64.  
  65.     // load the BWCC.DLL
  66.     BWCCGetVersion();
  67.  
  68.     // turn it back on.
  69.     SetErrorMode ( fuErr ) ;
  70.  
  71.     // check version of Windows. At the moment we support 3.1 or higher!
  72.     // this test courtesy of: davidds@microsoft.com (David D'Souza)
  73.     winVer = LOWORD(GetVersion());
  74.     winVer = (((WORD)(LOBYTE(winVer))) << 8)|(WORD)HIBYTE(winVer);
  75.     if (winVer < 0x030A)    // NOTE: Always use a HEX value here!!!
  76.     {     //exit
  77.         char szTemp[256];
  78.         LoadString( hInstance,
  79.                 (UINT)MAKEINTRESOURCE(IDS_BADVERSION),
  80.                 szTemp, sizeof szTemp );
  81.                 BWCCMessageBox (NULL, szTemp, szAppName,
  82.                 MB_ICONSTOP | MB_OK );
  83.         FreeLibrary ( hBWCCDLL );
  84.         return FALSE;
  85.     }
  86.  
  87.     // check command line options
  88.         // debug switch.
  89.     if ( (lstrcmpi(lpszCmdLine,"/d") == 0 ) ||
  90.          (lstrcmpi(lpszCmdLine,"-d") == 0 ) ) iDebugValue = DEBUG;
  91.  
  92.     // check for another instance.
  93.     if ( !hPrevInstance ) {
  94.  
  95.             WNDCLASS        wndclass;
  96.  
  97.             // first run - register class.
  98.         wndclass.style        = CS_HREDRAW | CS_VREDRAW;
  99.             wndclass.lpfnWndProc        = ExitWndProc;       // our WndProc.
  100.         wndclass.cbClsExtra        = 0;
  101.             wndclass.cbWndExtra         = DLGWINDOWEXTRA;    // IMPORTANT!!!
  102.         wndclass.hInstance        = hInstance;
  103.         wndclass.hIcon              = LoadIcon (hInstance, MAKEINTRESOURCE(IDI_EXIT1));
  104.         wndclass.hCursor        = LoadCursor (NULL, IDC_ARROW);
  105.         wndclass.hbrBackground      = (HBRUSH)(COLOR_WINDOW + 1);
  106.         wndclass.lpszMenuName    = MAKEINTRESOURCE(ExitWindowsMenu);
  107.         wndclass.lpszClassName    = szClassName;
  108.  
  109.         if (!RegisterClass (&wndclass))
  110.         return FALSE;
  111.     }
  112.         else {  // we have another instance running.
  113.         HWND hOldWnd;
  114.         char szTemp[256];
  115.  
  116.             // if there is an old window, find it and open it up.
  117.             if ( NULL != (hOldWnd = FindWindow( szClassName, "Exit Windows")) ) {
  118.                 // if its iconic, open it up.
  119.         if( IsIconic(hOldWnd) ) {
  120.                  if ( !OpenIcon ( hOldWnd ) ) {
  121.                            // error. Couldn't open iconized program.
  122.                LoadString( hInstance,
  123.                     (UINT)MAKEINTRESOURCE(IDS_BADOLDICON),
  124.                         szTemp, sizeof szTemp );
  125.                BWCCMessageBox (NULL, szTemp, szAppName,
  126.                     MB_ICONSTOP | MB_OK );
  127.                return FALSE;
  128.              }
  129.                 }
  130.                 // and bring it to the top.
  131.             if ( !BringWindowToTop( hOldWnd ) ) {
  132.                         // error. Couldn't bring window to top.
  133.                 LoadString( hInstance,
  134.                 (UINT)MAKEINTRESOURCE(IDS_BADWINDOWTOP),
  135.                 szTemp, sizeof szTemp );
  136.             BWCCMessageBox (NULL, szTemp, szAppName,
  137.                     MB_ICONSTOP | MB_OK );
  138.             return FALSE;
  139.         }
  140.                 // we found it OK - now bail out.
  141.         return FALSE ;
  142.         }
  143.         else
  144.         {   // error. We have a NULL from FindWindow call. 
  145.             LoadString( hInstance,
  146.                 (UINT)MAKEINTRESOURCE(IDS_BADOLDWINDOW),
  147.                 szTemp, sizeof szTemp );
  148.         BWCCMessageBox (NULL, szTemp, szAppName,
  149.                     MB_ICONSTOP | MB_OK );
  150.         return FALSE;
  151.             }
  152.     }
  153.  
  154.  
  155.     // and tell Windows to that our DialogProc is going to
  156.     // handle the dialog
  157.     hExitWnd = CreateDialog (hInstance, MAKEINTRESOURCE(ExitDialog),
  158.                 0, NULL);
  159.         // check window handle.
  160.         if ( !hExitWnd )
  161.                 return FALSE;
  162.  
  163.     // show it
  164.     ShowWindow (hExitWnd, nCmdShow);
  165.  
  166.         // load accelerators
  167.     hAccel = LoadAccelerators ( hInstance, MAKEINTRESOURCE(ExitAccelerators) );
  168.     if ( ! hAccel )
  169.         return FALSE;
  170.  
  171.         // message loop
  172.     while (GetMessage (&msg, NULL, 0, 0))
  173.         {
  174.          if ( !TranslateAccelerator ( hExitWnd, hAccel, &msg ) )
  175.                  {
  176.                  TranslateMessage (&msg);
  177.               DispatchMessage (&msg);
  178.                  }
  179.         }
  180.  
  181.  
  182.         // and exit.
  183.     return msg.wParam;
  184. }
  185.  
  186.  
  187. // -----------------------------------------------------------------------
  188.  
  189. /*
  190.  * AboutDlgProc - Routine to handle the About dialog window.
  191.  */
  192. #ifdef __BORLANDC__
  193. #pragma argsused
  194. #endif
  195. BOOL FAR PASCAL AboutDlgProc (HWND hDlg, UINT iMessage, UINT wParam, LONG lParam)
  196. {
  197.  
  198.    // set focus to OK button so that the keyboard works.
  199.    SetFocus ( GetDlgItem ( hDlg, IDOK ) );
  200.    // display credits.
  201.    if ((iMessage == WM_COMMAND) && (wParam == IDOK))
  202.       EndDialog(hDlg, 0);    // dismiss dialog if OK
  203.  
  204.    return(0);             // otherwise just sit there
  205. }
  206.  
  207.  
  208.  
  209. // -------------------------------------------------------------------------
  210.  
  211. /*
  212.  * ExitWndProc - Routine to handle main window
  213.  *
  214.  * All messages that we do NOT process, go to the BWCCDefWindowProc
  215.  * which will handle them...
  216.  *
  217.  */
  218.  
  219. LRESULT FAR PASCAL _export ExitWndProc (HWND hDlg, UINT iMessage, UINT wParam, LONG lParam)
  220. {
  221.     static HMENU    hSysMenu;
  222.     FARPROC        lpfnAboutDlgProc;
  223.     static BOOL    fbWriteIni = FALSE ;
  224.         static char     szMenuExitOption[25];
  225.  
  226.  
  227.     switch (iMessage)
  228.     {
  229.  
  230.  
  231.             // create window and fix the system menu
  232.         case WM_CREATE:
  233.         // get the handle to the menu
  234.             hSysMenu = GetSystemMenu(hDlg,FALSE);
  235.                 // add separator line and quick exit.
  236.         AppendMenu(hSysMenu, MF_SEPARATOR, 900, NULL);
  237.                 // default exit mode
  238.         iBootAction = IDD_EXIT;
  239.         // get default exit mode
  240.         if ( IDD_EXIT != ( iBootAction += GetPrivateProfileInt (
  241.                         szSection, szIconExit,
  242.                         0, szIniFile)) )
  243.         {
  244.                     // INI file sanity check.
  245.             if ( iBootAction < IDD_EXIT ||
  246.                  iBootAction > IDD_BOOT ) iBootAction = IDD_EXIT;
  247.                     // we saved the flag (0,1,2) only, *not* the IDD_VALUE.
  248.             // iBootAction += IDD_EXIT;
  249.         }
  250.         // prepare menu strings. The menu item itself will be
  251.         // added during the WM_INIEXIT message, when we are sure
  252.         // that the dialog has been created already.
  253.         switch ( iBootAction )
  254.         {
  255.             case IDD_EXIT:
  256.              wsprintf(szMenuExitOption, "Exit &DOS" );
  257.              break;
  258.  
  259.             case IDD_REST:
  260.              wsprintf(szMenuExitOption, "Restart &Windows" );
  261.              break;
  262.  
  263.                     case IDD_BOOT:
  264.              wsprintf(szMenuExitOption, "Reboot &System" );
  265.              break;
  266.  
  267.         }
  268.  
  269.         // No add the Quick Exit (no questions asked)
  270.         AppendMenu(hSysMenu, MF_STRING, IDM_QUICKEXIT, "&Quick Exit");
  271.                 // check last state, and act accordingly.
  272.         if ( EW_SLOW != ( iQuickExitValue = GetPrivateProfileInt(szSection,
  273.                     szQuickExit, EW_SLOW, szIniFile))) {
  274.             // sanity check.
  275.                     iQuickExitValue = EW_QUICK;
  276.             CheckMenuItem(hSysMenu, IDM_QUI